package com.ruoyi.web.controller.warehouse;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.biz.WarehouseService;
import com.ruoyi.system.domain.DeliveryOrder;
import com.ruoyi.system.request.DeliveryOrderAdd;
import com.ruoyi.system.request.DeliveryOrderListRequest;
import com.ruoyi.system.response.OutboundDetailList;
import com.ruoyi.system.service.IDeliveryOrderService;
import com.ruoyi.system.service.IOutboundInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 出库单Controller
 *
 * @author tanshen
 * @date 2023-08-21
 */
@Controller
@RequestMapping("/warehouse/deliveryOrder")
public class DeliveryOrderController extends BaseController {
    private String prefix = "warehouse/deliveryOrder";

    @Autowired
    private IDeliveryOrderService deliveryOrderService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private IOutboundInfoService outboundInfoService;

    @RequiresPermissions("warehouse:deliveryOrder:view")
    @GetMapping()
    public String deliveryOrder() {
        return prefix + "/deliveryOrder";
    }

    /**
     * 查询出库单列表
     */
    @RequiresPermissions("warehouse:deliveryOrder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DeliveryOrderListRequest request) {
        startPage();
        List<DeliveryOrder> list = deliveryOrderService.selectDeliveryOrderList(request);
        return getDataTable(list);
    }

    @GetMapping("/detailList/{deliveryOrderId}")
    @ResponseBody
    public TableDataInfo detailList(@PathVariable String deliveryOrderId) {
        startPage();
        List<OutboundDetailList> list = outboundInfoService.detailList(deliveryOrderId);
        return getDataTable(list);
    }

    /**
     * 导出出库单列表
     */
    @RequiresPermissions("warehouse:deliveryOrder:export")
    @Log(title = "出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DeliveryOrderListRequest request) {
        List<DeliveryOrder> list = deliveryOrderService.selectDeliveryOrderList(request);
        ExcelUtil<DeliveryOrder> util = new ExcelUtil<DeliveryOrder>(DeliveryOrder.class);
        return util.exportExcel(list, "出库单数据");
    }

    /**
     * 新增出库单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        String orderId = "CK" + "08" + DateUtils.parseDateToStr("yyMMddHHmmss", new Date());
        mmap.put("orderId", orderId);
        return prefix + "/add";
    }

    /**
     * 新增保存出库单
     */
    @RequiresPermissions("warehouse:deliveryOrder:add")
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DeliveryOrderAdd deliveryOrder) {
        return toAjax(warehouseService.addDeliveryOrder(deliveryOrder));
    }

    /**
     * 修改出库单
     */
    @RequiresPermissions("warehouse:deliveryOrder:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        DeliveryOrder deliveryOrder = deliveryOrderService.selectDeliveryOrderById(id);
        mmap.put("deliveryOrder", deliveryOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存出库单
     */
    @RequiresPermissions("warehouse:deliveryOrder:edit")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DeliveryOrder deliveryOrder) {
        return toAjax(deliveryOrderService.updateDeliveryOrder(deliveryOrder));
    }

    /**
     * 删除出库单
     */
    @RequiresPermissions("warehouse:deliveryOrder:remove")
    @Log(title = "出库单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(deliveryOrderService.deleteDeliveryOrderByIds(ids));
    }
}
